package com.umakr.extjstext.controller;

import com.umakr.extjstext.dao.UserDao;
import com.umakr.extjstext.dto.UserDto;
import com.umakr.extjstext.entity.Class;
import com.umakr.extjstext.entity.User;
import com.umakr.extjstext.repository.ClassRepository;
import com.umakr.extjstext.repository.UserRepository;
import com.umakr.extjstext.tree.Tree;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.*;

/**
 * Created by Mikey on 2016/11/12.
 */
@Controller
public class indexController {

    private String USERMAPPER = "com.umakr.extjstext.mapper.UserMapper";
    private String CLASSMAPPER = "com.umakr.extjstext.mapper.ClassMapper";

    @Autowired
    UserRepository userRepository;

    @Autowired
    ClassRepository classRepository;

    @Autowired
    SqlSessionTemplate sqlSessionTemplate;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/getUser.json")
    @ResponseBody
    public List<UserDao> getUser(String key, String value){
        Map<String, Object> map = new HashMap<String, Object>();
        List<UserDto> users = new ArrayList<>();
        if(key.equals("0")){
            users = sqlSessionTemplate.selectList(USERMAPPER+".findAll");
        }
        if(key.equals("1")){
            Class c = classRepository.findOne(value);
            if(c.getCp() == null||"".equals(c.getCp())){
                map.put("CPID",value);
                List<String> cids = sqlSessionTemplate.selectList(CLASSMAPPER+".findCidByCp",map);
                map.put("item",cids);
                users = sqlSessionTemplate.selectList(USERMAPPER+".findAllByTypeList",map);
            }else{
                map.put("TYPE",value);
                users = sqlSessionTemplate.selectList(USERMAPPER+".findAllByType",map);
            }

        }
        List<UserDao> userDaos = new ArrayList<>();
        for(UserDto user : users){
            UserDao userDao = new UserDao();
            BeanUtils.copyProperties(user,userDao);
            if(user.getType() != null&&!"".equals(user.getType())){
                Class c = classRepository.findOne(user.getType());
                Class cc = classRepository.findOne(c.getCp());
                userDao.setCid(c.getCid());
                userDao.setCname(c.getCname());
                userDao.setNid(cc.getCid());
                userDao.setNname(cc.getCname());
            }
            userDaos.add(userDao);
        }
        return userDaos;
//        return null;
    }

    @RequestMapping("/saveUser.json")
    @ResponseBody
    public Map saveUser(UserDao userDao){
        Map map = new HashMap();
        User user = new User();
        if(userDao.getId() == null||"".equals(userDao.getId())){
            userDao.setId(UUID.randomUUID().toString());
            map.put("success","添加成功");
        }else{
            user = userRepository.findOne(userDao.getId());
            map.put("success","修改成功");
        }
        BeanUtils.copyProperties(userDao,user);
        user.setType(userDao.getCid());
        userRepository.saveAndFlush(user);
        return map;
    }

    @RequestMapping("/deleteUser.json")
    @ResponseBody
    public String deleteUser(String id){
        String map;
        userRepository.delete(id);
        map = "删除成功";
        return map;
    }

    @RequestMapping("/getClass.json")
    @ResponseBody
    public List<Tree> getClasses(){
        List<Class> classes = classRepository.findAll();
        return Tree.makeTree(classes);
    }
}
